/*
 * @lc app=leetcode.cn id=1417 lang=javascript
 *
 * [1417] 重新格式化字符串
 */

// @lc code=start
/**
 * @param {string} s
 * @return {string}
 */
var reformat = function (s) {
  const s1 = [], s2 = [];
  s.split('').forEach(ch => {
    if (ch >= '0' && ch <= '9') {
      s1.push(ch);
    } else {
      s2.push(ch);
    }
  });
  if (Math.abs(s1.length - s2.length) > 1) {
    return '';
  }
  const res = [];
  for (let i = 0; i < Math.min(s1.length, s2.length); i++) {
    res.push(s1[i], s2[i]);
  }
  if (s1.length < s2.length) {
    return s2[s2.length - 1] + res.join('');
  }
  if (s1.length > s2.length) {
    return res.join('') + s1[s1.length - 1];
  }
  return res.join('');
};
// @lc code=end
